/**
 * The appointment class is used as a container to hold information
 * on appointments. It also is used to create new appointments and to
 * query appointments of clients and workers.
 *
 * @author Jeremiah Weeden
 * @version 4/14/2007
 */

package com.elderhealth.schedule;

import java.sql.Timestamp;
import com.elderhealth.client.Client;
import com.elderhealth.worker.HomecareWorker;
import com.elderhealth.util.DBManager;
import com.elderhealth.util.Log;

public class Appointment
{
    /* instance variables */
    private int appointmentID;
    private int boundClientID;
    private int boundWorkerID;
    private Timestamp startTime;
    private Timestamp endTime;

    /**
     * Constructor for objects of class Appointment
     */
    public Appointment()
    {
      /* initialise instance variables */
      appointmentID = -1;
      boundClientID = -1;
      boundWorkerID = -1;
      startTime = null;
      endTime = null;
    }

    /**
     * Constructor for objects of class Appointment
     *
     * @param apptID    appointment id
     *        cID       client id
     *        hwID      homecareworker id
     *        start     start time for the appointment
     *        end       end time for the appointment
     */
    public Appointment(int apptID, int cID, int hwID,
                Timestamp start, Timestamp end)
    {
      /* initialise instance variables */
      appointmentID = apptID;
      boundClientID = cID;
      boundWorkerID = hwID;
      startTime = start;
      endTime = end;
    }

    /* Static Method Section */

    /**
     * getAppointmentsByClient calls the DBManager class to get a client's
     * appointments between a specific set of timestamps
     *
     * @param  clientID     the client id to get appointments for
     *         rangeStart   the timestamp to bound appointments
     *         rangeEnd     the timestamp to bound appointments
     * @return Appointment[] appts
     */
    public static Appointment[] getAppointmentsByClient(int clientID,
                Timestamp rangeStart, Timestamp rangeEnd)
    {
      /* Initialize variables */
      Appointment[] appts = null;
      DBManager db = DBManager.getInstance();
      Log logFile = Log.getInstance();

      try
      {
        /* Call the DBManager class to get appointments */
        appts = db.getAppointmentsByClient(clientID,rangeStart,rangeEnd);
        logFile.debug("Appointment: getAppointmentsByClient: " +
            "Query for appointments by client completed.");
        if (appts != null) logFile.debug("Appointment: getAppointments" +
            "ByClient: Appointments found. Found " + appts.length +
            " appointments.");
        return appts;
      }
      catch (Exception e)
      {
        logFile.error("An exception occurred. Exception is: " + e.toString());
        return appts;
      }
    }

    /**
     * getAppointmentsByWorker calls the DBManager class to get a worker's
     * appointments between a specific set of timestamps
     *
     * @param  workerID     the worker id to get appointments for
     *         rangeStart   the timestamp to bound appointments
     *         rangeEnd     the timestamp to bound appointments
     * @return Appointment[] appts
     */
    public static Appointment[] getAppointmentsByWorker(int workerID,
                Timestamp rangeStart,Timestamp rangeEnd)
    {
      /* Initialize variables */
      Appointment[] appts = null;
      DBManager db = DBManager.getInstance();
      Log logFile = Log.getInstance();

      try
      {
        /* Call the DBManager class to get appointments */
        appts = db.getAppointmentsByWorker(workerID,rangeStart,rangeEnd);
        logFile.debug("Appointment: getAppointmentsByWorker: " +
            "Query for appointments by worker completed.");
        if (null != appts) logFile.debug("Appointment: getAppointments" +
            "ByWorker: Appointments found. Found " + appts.length +
            " appointments.");
        return appts;
      }
      catch (Exception e)
      {
        logFile.error("Appointment: getAppointmentsByWorker: An " +
            "exception occurred. Exception is: " + e.toString());
        return appts;
      }
    }

    /**
     * createAppointment creates an appointment between a HomecareWorker and
     * a client. It calls the DBManager class to persist the information
     * including the start and end dates.
     *
     * @param  clientID     the client id to set an appointment for
     *         workerID     the worker id to set an appointment for
     *         start        the timestamp to bound the appointment
     *         end          the timestamp to bound the appointment
     * @return Appointment appt
     */
    public static Appointment createAppointment(int clientID, int workerID,
                Timestamp start, Timestamp end)
    {
      /* Initialize variables */
      Appointment newAppt = null;
      DBManager db = DBManager.getInstance();
      Log logFile = Log.getInstance();

      try
      {
        /* Call the DBManager class to create the appointment */
        newAppt = db.setAppointment(clientID,workerID,start,end);
        if (null != newAppt) logFile.debug("Appointment: " +
            "createAppointment: Appointment created.");
        return newAppt;
      }
      catch (Exception e)
      {
        logFile.error("Appointment: createAppointment: " +
            "An exception occurred. Exception is: " + e.toString());
        return null;
      }
    }

    /* Method Section */

    /**
     * deleteAppointment removes an appointment (based off appointment ID)
     *
     * @return  boolean success  if appointment was successfully deleted
     */
    public boolean deleteAppointment()
    {
      /* Initialize variables */
      boolean success = false;
      DBManager db = DBManager.getInstance();
      Log logFile = Log.getInstance();

      try
      {
        /* Call the DBManager to remove the appointment */
        success = db.deleteAppointment(this);
        logFile.debug("Appointment: deleteAppointment: " +
            "Appointment deleted?: " + success);
        return success;
      }
      catch (Exception e)
      {
        logFile.error("Appointment: deleteAppointment: " +
            "An exception occurred. Exception is: " + e.toString());
        return true;
      }
    }

    /**
     * getClient returns the client that is bound to this appointment
     *
     * @return Client client
     */
    public Client getClient ()
    {
      /* Initialize variables */
      Client client = null;
      Log logFile = Log.getInstance();

      try
      {
        /* Call the Client class to retrieve the client */
        logFile.debug("Appointment: getClient: Calling " +
            "Client.getClientById with identifier: " + boundClientID);
        client = Client.getClientById(boundClientID);
        if (null != client) logFile.debug("Appointment: getClient: " +
            "Client returned.");
        return client;
      }
      catch (Exception e)
      {
        logFile.error("Appointment: getClient: An exception " +
            "occurred. Exception is: " + e.toString());
        return null;
      }
    }

    /**
     * getWorker returns the worker that is bound to this appointment
     *
     * @return HomecareWorker worker
     */
    public HomecareWorker getWorker ()
    {
       /* Initialize variables */
       HomecareWorker worker = null;
       Log logFile = Log.getInstance();

       try
       {
          /* Call the HomecareWorker class to retrieve the worker */
          logFile.debug("Appointment: getWorker: Calling " +
              "Worker.getWorkerById with identifier: " + boundWorkerID);
          worker = HomecareWorker.getWorkerById(boundWorkerID);
          if (null != worker) logFile.debug("Appointment: getWorker: " +
              "Worker returned.");
          return worker;
        }
        catch (Exception e)
        {
          logFile.error("Appointment: getWorker: An exception " +
              "occurred. Exception is: " + e.toString());
          return null;
        }
    }

    /* Accessor Method */

    /**
     * getAppointmentID gets the appointment id
     *
     * @return appointmentID
     */

    public int getAppointmentID()
    {
    	return appointmentID;
    }

    /**
     * getStartTime gets the start date of this appointment
     *
     * @return startTime
     */
    public Timestamp getStartTime ()
    {
      return startTime;
    }

    /**
     * setstartTime sets the start date of this appointment
     *
     * @param start timestamp representing start of appointment
     */
    public void setStartTime (Timestamp start)
    {
      startTime = start;
    }

    /**
     * getEndTime gets the end date of this appointment
     *
     * @return end
     */
    public Timestamp getEndTime ()
    {
      return endTime;
    }

    /**
     * setEndTime sets the end date of this appointment
     *
     * @param endDate timestamp representing end of appointment
     */
    public void setEndTime (Timestamp end)
    {
      endTime = end;
    }
}